﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     Keyword业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class KeywordBLL
    {
        private static KeywordBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IKeywordDao dao = null;

        private KeywordBLL()
        {
            dao = KeywordSqlMapDao.Instance;
        }

        public static KeywordBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new KeywordBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<Keyword> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<Keyword> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<Keyword> GetListByParameters(object TopCounts, Keyword keyword)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<Keyword> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="KeyID"></param>
        /// <returns></returns>
        public Keyword GetDetail(System.String keyid)
        {
            try
            {
                ///daoManager.BeginTransaction();
                Keyword keyword = new Keyword();
                keyword.KeyID = keyid;
                return dao.GetDetail(keyword);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 根据关键字的内容获取关键字信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public Keyword GetDetailByContent(string key)
        {
            try
            {
                Keyword keyword = new Keyword();
                keyword.Content = key;
                ///daoManager.BeginTransaction();
                return dao.GetDetail(keyword);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public object Add(Keyword keyword)
        {
            try
            {
                return dao.Add(keyword);
            }
            catch 
            {
                
                throw;
            }
        }

        /// <summary>
        /// 添加关键字（已存在则返回KeyID，不存在则添加新项，返回新KeyID)
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns>返回KeyID</returns>
        public string AddKey(Keyword keyword)
        {
            try
            {
                //daoManager.BeginTransaction();
                //检查关键字是否已存在
                Keyword ret=dao.GetDetail(keyword);
                if (ret != null)
                {
                    return ret.KeyID;
                }
                else
                {
                    //添加
                    keyword.KeyID = Guid.NewGuid().ToString();
                    keyword.State = (int)Common.Enum.auvState.Enabled;
                    keyword.IsDel = (int)Common.Enum.auvDel.NotDel;
                    keyword.CreateDate = DateTime.Now;
                    keyword.UpdateDate = DateTime.Now;
                    keyword.BeginDate = DateTime.Now;
                    keyword.EndDate = DateTime.Now;
                    dao.Add(keyword);
                    return keyword.KeyID;
                }
                //daoManager.CommitTransaction();
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public int Update(Keyword keyword)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(keyword);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="KeyID"></param>
        /// <returns></returns>
        public int Delete(System.String keyid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(keyid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public int Delete(Keyword keyword)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(keyword);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        #region##通过服务ID得到关键字集合
        /// <summary>
        /// 通过服务ID得到关键字集合
        /// 创建人：Wilson
        /// 创建时间：2011-10-25
        /// </summary>
        /// <param name="serviceId"></param>
        /// <returns></returns>
        public IList<AUV5.Model.Keyword> GetKeyListByServiceId(string serviceId)
        {
            try
            {
                IList<AUV5.Model.Keyword> list = new List<AUV5.Model.Keyword>();   
                AUV5.Model.Keyword key = null;

                IList<AUV5.Model.ServiceKeyRela> relaList = ServiceKeyRelaBLL.Instance.GetKeysByServiceID(null, serviceId);  //得到十个关键字关系

                foreach (AUV5.Model.ServiceKeyRela rela in relaList)
                {
                    key = new Keyword();

                    key = GetDetail(rela.KeyID);

                    list.Add(key);
                }

                return list;
            }
            catch 
            {
                
                throw;
            }
        }
        #endregion


        /// <summary>
        /// 获取类别中的最热关键字
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public IList<string> GetHotKeyByCategory(string category)
        {
            try
            {
                ServiceCategory servicecategory = ServiceCategoryBLL.Instance.GetCategoryByIDOrCode(category);
                if (servicecategory != null)
                {
                    Hashtable ht = new Hashtable();
                    ht.Add("categoryID", servicecategory.CategoryID);
                    ht.Add("topCounts", 10);
                    return  dao.GetHotKey(ht);
                }
                else
                {
                    return null;
                }
            }
            catch 
            {
                
                throw;
            }
        }

        /// <summary>
        /// 获取最热关键字
        /// </summary>
        /// <returns></returns>
        public IList<string> GetHotestKey()
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("topCounts", 10);
                return dao.GetHotKey(ht);
            }
            catch 
            {
                
                throw;
            }
        }

        
    }
}
